﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace CS544_Protocol_Client
{
    /**
    * Author: Kelly Page, Brian Driscoll
    * Date Created: 2/23/2011
    * Date Updated: 3/07/2011
    * 
    * Purpose: Handles protocol state for chat client.
    * 
    **/
    public class DFA
    {

        public State ProtocolState { get; private set; }//holds state 

       
        //Constructor
        public DFA()
        {
            this.setClosed();
        }      



        //process message type to set protocol state
        public void processMessage(Message serverMessage)
        {

            Message msg = serverMessage;


            switch(msg.Type)
            {
                case MessageType.REGISTER:
                        {
                            this.ProtocolState = State.REGISTERING;
                            break;
                        }       
                case MessageType.REGISTER_ACK:
                    {
                        this.ProtocolState = State.CLOSED;
                        break;
                    }
                case MessageType.REGISTER_NAK:
                    {
                        this.ProtocolState = State.CLOSED;
                        break;
                    }
                case MessageType.LOGIN:
                    {
                        this.ProtocolState = State.AUTHENTICATING;
                        break;
                    }
                case MessageType.LOGIN_ACK:
                    {
                        this.ProtocolState = State.OPEN;
                        break;
                    }
                case MessageType.LOGIN_NAK:
                    {
                        this.ProtocolState = State.CLOSED;
                        break;
                    }
                case MessageType.LOGOUT:
                    {
                        this.ProtocolState = State.CLOSING;
                        break;
                    }
                case MessageType.LOGOUT_ACK:
                    {
                        this.ProtocolState = State.CLOSED;
                        break;
                    }
                case MessageType.LOGOUT_NAK:
                    {
                        this.ProtocolState = State.OPEN;
                        break;
                    }

                   
            }

            Debug.WriteLine(ProtocolState);
        }




        //Set state to closed
        private void setClosed()
        {
            this.ProtocolState = State.CLOSED;

        }


    }
}
